package de.shortys.myhome.api.navigation;

import de.shortys.myhome.api.controller.AbstractModule;
import de.shortys.myhome.api.controller.AbstractSubModule;

public class ModuleNavigator {
	private AbstractSubModule activeSubModule;

	public boolean isActive(AbstractSubModule activeSubModule) {
		if (this.activeSubModule != null) {
			return this.activeSubModule.equals(activeSubModule);
		}
		return false;
	}

	public void setActive(AbstractSubModule newActiveSubModule) {
		if (activeSubModule != null && !activeSubModule.isChild(newActiveSubModule)) {
			activeSubModule.deactivate();
			activeSubModule.getRoot().getView().collapse(activeSubModule.getNode());
		} else if (activeSubModule != null && activeSubModule.isChild(newActiveSubModule)) {
			activeSubModule.deactivate();
		}

		if (activeSubModule != null && activeSubModule.getParent() instanceof AbstractSubModule
				&& newActiveSubModule.getParent() instanceof AbstractModule) {
			AbstractSubModule parent = (AbstractSubModule) activeSubModule.getParent();
			parent.getRoot().getView().collapse(parent.getNode());
		}

		activeSubModule = newActiveSubModule;
		activeSubModule.getRoot().getView().expand(activeSubModule.getNode());

	}

	public AbstractSubModule getActive() {
		return activeSubModule;
	}

	public void clear() {
		activeSubModule = null;
	}
}
